package com.gsn.engine.tween;

import aurelienribon.tweenengine.TweenAccessor;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.gsn.engine.gdx.GsnRotationImage;

public class ActorAccessor implements TweenAccessor<Actor> {
	public static final int POS_XY = 1;
	public static final int CPOS_XY = 2;
	public static final int SCALE_XY = 3;
	public static final int ROTATION = 4;
	public static final int OPACITY = 5;
	public static final int TINT = 6;
	public static final int ROTATION_CENTER = 7;

	@Override
	public int getValues(Actor target, int tweenType, float[] returnValues) {
		switch (tweenType) {
		case POS_XY:
			returnValues[0] = target.x;
			returnValues[1] = target.y;
			return 2;

		case CPOS_XY:
			returnValues[0] = target.x + target.width * target.scaleX / 2;
			returnValues[1] = target.y + target.height * target.scaleY / 2;
			return 2;

		case SCALE_XY:
			returnValues[0] = target.scaleX;
			returnValues[1] = target.scaleY;
			return 2;

		case ROTATION:
			returnValues[0] = target.rotation;
			return 1;
		case OPACITY:
			returnValues[0] = target.color.a;
			return 1;

		case TINT:
			returnValues[0] = target.color.r;
			returnValues[1] = target.color.g;
			returnValues[2] = target.color.b;
			return 3;
		case ROTATION_CENTER:
			returnValues[0]= target.rotation;
			return 1;
		default:
			assert false;
			return -1;
		}
	}

	@Override
	public void setValues(Actor target, int tweenType, float[] newValues) {
		switch (tweenType) {
		case POS_XY:
			//target.setPosition(newValues[0], newValues[1]);
			target.x = newValues[0];
			target.y = newValues[1];
			break;
		case CPOS_XY:
			//target.setPosition(newValues[0] - target.getWidth() / 2, newValues[1] - target.getHeight() / 2);
			target.x = newValues[0] - target.width * target.scaleX / 2;
			target.y = newValues[1] - target.height * target.scaleY / 2;
			break;
		case SCALE_XY:
			target.scaleX = newValues[0];
			target.scaleY = newValues[1];
			break;
		case ROTATION:
			target.rotation = newValues[0];
			break;

		case OPACITY:
			Color c = target.color;
			c.set(c.r, c.g, c.b, newValues[0]);			
			break;

		case TINT:
			c = target.color;
			c.set(newValues[0], newValues[1], newValues[2], c.a);
			//target.setColor(c);
			break;
		case ROTATION_CENTER:
			target.rotation = newValues[0];
			GsnRotationImage tmp = (GsnRotationImage)target;
			double deg = newValues[0];
			double radian =  deg * (Math.PI) / 180d; 
			target.x =  (float) (tmp.getCenX() - (target.width * Math.cos(radian) + -target.height * Math.sin(radian)) / 2);
			target.y =  (float) (tmp.getCenY() - (target.width * Math.sin(radian) + target.height * Math.cos(radian)) / 2);		
			//System.out.println("(" + target.x + ", " + target.y + ")");
			break;
		default:
			assert false;
		}
	}
}
